package com.baidu.videoseries.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;

/**
 * Created by leiyun on 2018/6/5.
 */
public class PlayerShutter extends View {
    public static final int TYPE_PROGRESS = 1;
    public static final int TYPE_LIGHT = 2;
    public static final int TYPE_VOICE = 3;
    private Paint virPaint;
    private Paint solPaint;

    private int psMarginLeft;
    private int psMarginRight;
    private int psMarginBottom;

    private int psMaxVoice;
    private int psMaxLight;
    private int psMaxProgress;
    private int psVoice;
    private int psProgress;
    private int psLight;
    private int mHorSpace;
    private int mVerSpace;


    private int validWidth;
    private int validHeight;
    private float virLineWidth;
    private float solLineWidth;

    private int maxBottom;
    private int mProgressBottom;
    private int mProgressLeft;
    private int mLightLeft;
    private int mLightBottom;
    private int mVoiceRight;
    private int mVoiceBottom;

    private RectF mLightArea;
    private RectF mVoiceArea;
    private RectF mProgressArea;
    private RectF mRect;
    private int mAreaGap;

    public void setMaxVoice(int psMaxVoice) {
        this.psMaxVoice = psMaxVoice;
        postInvalidate();
    }

    public void setMaxLight(int psMaxLight) {
        this.psMaxLight = psMaxLight;
        postInvalidate();
    }

    public void setMaxProgress(int psMaxProgress) {
        this.psMaxProgress = psMaxProgress;
        postInvalidate();
    }

    public void setVoice(int psVoice) {
        this.psVoice = psVoice;
        postInvalidate();
    }

    public void setProgress(int psProgress) {
        this.psProgress = psProgress;
        invalidate();
    }

    public void setLight(int psLight) {
        this.psLight = psLight;
        postInvalidate();
    }

    public int getMaxVoice() {
        return psMaxVoice;
    }

    public int getMaxLight() {
        return psMaxLight;
    }

    public int getMaxProgress() {
        return psMaxProgress;
    }

    public int getVoice() {
        return psVoice;
    }

    public int getProgress() {
        return psProgress;
    }

    public int getLight() {
        return psLight;
    }

    private GestureDetector mDetector;

    public PlayerShutter(Context context) {
        this(context, null, 0);

    }

    public PlayerShutter(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public PlayerShutter(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        //init attr
        this.psMarginLeft = 20;
        this.psMarginRight = 20;
        this.psMarginBottom = 50;

        this.mVerSpace = 20;
        this.mHorSpace = 30;

        this.mAreaGap = 20;
        this.virLineWidth = 1;
        this.solLineWidth = 1;

        this.psMaxProgress=100;
        this.psMaxVoice=10;
        this.psMaxLight=10;

        //init paint
        this.mDetector = new GestureDetector(getContext(), new GestureListener());
        this.virPaint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.DITHER_FLAG);
        this.virPaint.setColor(Color.GRAY);

        this.solPaint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.DITHER_FLAG);
        this.solPaint.setColor(Color.RED);

        //init valid area
        this.mLightArea = new RectF();
        this.mVoiceArea = new RectF();
        this.mProgressArea = new RectF();

        this.mRect=new RectF();
    }


    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        this.validWidth = w - getPaddingLeft() - getPaddingRight();
        this.validHeight = h - getPaddingTop() - getPaddingBottom();
        this.maxBottom = h - getPaddingBottom();
        this.mProgressLeft = getPaddingLeft()+mHorSpace;
        this.mProgressBottom = maxBottom - psMarginBottom;

        this.mLightLeft = getPaddingLeft() + psMarginLeft;
        this.mLightBottom = h - getPaddingBottom() - mVerSpace;

        this.mVoiceRight = w - getPaddingRight() - psMarginRight;
        this.mVoiceBottom = h - getPaddingBottom() - mVerSpace;

        //progress valid click area
        mProgressArea.left = mProgressLeft;
        mProgressArea.right = mProgressArea.left + validWidth - 2 * mHorSpace;
        mProgressArea.top = mProgressBottom - mAreaGap;
        mProgressArea.bottom = mProgressBottom + mAreaGap;

        //light valid click area
        mLightArea.left = mLightLeft - mAreaGap;
        mLightArea.right = mLightLeft + mAreaGap;
        mLightArea.bottom = mLightBottom;
        mLightArea.top = mLightArea.bottom - validHeight + 2 * mVerSpace;

        //voice area
        mVoiceArea.left = mVoiceRight - mAreaGap;
        mVoiceArea.right = mVoiceRight + mAreaGap;
        mVoiceArea.bottom = mVoiceBottom;
        mVoiceArea.top = mVoiceArea.bottom - validHeight + 2 * mVerSpace;

    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        //draw progress
        drawBar(TYPE_PROGRESS, canvas);
        //draw light bar
        drawBar(TYPE_LIGHT, canvas);
        //draw voice bar
        drawBar(TYPE_VOICE, canvas);
    }

    /**
     * draw bar
     *
     * @param type
     * @param canvas
     */
    private void drawBar(int type, Canvas canvas) {
        if (type == TYPE_PROGRESS) {
            mRect.left = mProgressLeft + virLineWidth;
            mRect.right = mRect.left + validWidth - 2 * virLineWidth - 2 * mHorSpace;
            mRect.top = mProgressBottom - virLineWidth;
            mRect.bottom = mProgressBottom - virLineWidth;
            drawLine(mRect, canvas, virPaint);
            mRect.left = mProgressLeft + solLineWidth;
            mRect.right = mRect.left + (validWidth - 2 * solLineWidth - 2 * mHorSpace) * psProgress / psMaxProgress;
            mRect.top = mProgressBottom - solLineWidth;
            mRect.bottom = mProgressBottom - solLineWidth;
            drawLine(mRect, canvas, solPaint);
            return;
        }
        if (type == TYPE_LIGHT) {
            mRect.left = mLightLeft + virLineWidth;
            mRect.right = mRect.left;
            mRect.bottom = mLightBottom - virLineWidth;
            mRect.top = mRect.bottom - (validHeight - 2 * virLineWidth - 2 * mVerSpace);
            drawLine(mRect, canvas, virPaint);
            mRect.left = mLightLeft + solLineWidth;
            mRect.right = mRect.left;
            mRect.bottom = mLightBottom - solLineWidth;
            mRect.top = mRect.bottom - (validHeight - 2 * solLineWidth - 2 * mVerSpace) * psLight / psMaxLight;
            drawLine(mRect, canvas, solPaint);
            return;
        }
        if (type == TYPE_VOICE) {
            mRect.left = mVoiceRight - virLineWidth;
            mRect.right = mRect.left;
            mRect.bottom = mVoiceBottom - virLineWidth;
            mRect.top = mRect.bottom - (validHeight - 2 * virLineWidth - 2 * mVerSpace);
            drawLine(mRect, canvas, virPaint);
            mRect.left = mVoiceRight - virLineWidth;
            mRect.right = mRect.left;
            mRect.bottom = mVoiceBottom - solLineWidth;
            mRect.top = mRect.bottom - (validHeight - 2 * solLineWidth - 2 * mVerSpace) * psLight / psMaxLight;
            drawLine(mRect, canvas, solPaint);
        }
    }

    /**
     * draw line
     *
     * @param rect
     * @param canvas
     * @param paint
     */
    private void drawLine(RectF rect, Canvas canvas, Paint paint) {
        canvas.drawLine(rect.left, rect.top, rect.right, rect.bottom, paint);
    }


    @Override
    public boolean onTouchEvent(MotionEvent event) {
        return mDetector.onTouchEvent(event);
    }

    /**
     * gesture listener
     */
    class GestureListener implements GestureDetector.OnGestureListener {
        @Override
        public boolean onDown(MotionEvent e) {
            return true;
        }

        @Override
        public void onShowPress(MotionEvent e) {

        }

        @Override
        public boolean onSingleTapUp(MotionEvent e) {
            return false;
        }

        @Override
        public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
            return false;
        }

        @Override
        public void onLongPress(MotionEvent e) {

        }

        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
            return false;
        }
    }
}
